package littlewhisker.components.ui.scrollbar {
	import flash.events.MouseEvent;
	import littlewhisker.globals.constants.eventtypes.UIEventTypes;
	import littlewhisker.communication.RequestEvent;
	import flash.geom.Rectangle;

	import littlewhisker.globals.constants.errortypes.UIErrorTypes;
	import littlewhisker.managers.ErrorManager;

	import flash.display.Sprite;

	import littlewhisker.components.ui.misc.DragableItem;
	import flash.events.Event;

	import littlewhisker.components.ui.AbstractUIComponent;

	/**
	 * @author Philip
	 */
	public class AbstractScrollbar extends AbstractUIComponent {
		
		private var _thumbAsset : Sprite;
		private var _trackAsset : Sprite;
		private var _percent : Number = 0;
		
		private var thumb:DragableItem;
		private var area:Rectangle;
		private var upperLimit:Number;
		private var range:Number;
		
		public function get percent() : Number {
			return _percent;
		}
		
		public function set percent(percent : Number) : void {
			_percent = percent;
			thumbAsset.y = (range * percent);
			dispatch();
		}
		
		public function get parent() : Sprite {
			return asset;
		}
		
		public function set parent(parent : Sprite) : void {
			asset = parent;
		}
		
		public function get thumbAsset() : Sprite {
			return _thumbAsset;
			
		}
		
		public function set thumbAsset(thumbAsset : Sprite) : void {
			
			if(_thumbAsset){
				 ErrorManager.throwError(UIErrorTypes.ASSIGN_MULTIPLE_ASSETS) 
			} else {
				 _thumbAsset = thumbAsset;
				thumb = new DragableItem(thumbAsset, area);
				thumb.addEventListener(UIEventTypes.ON_MOVE, onScroll);
				thumb.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
				thumb.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
				if(trackAsset){
					setLimits();
				}
				 
               
            }
			
		}
		
		private function onMouseUp( event:MouseEvent ):void{
			dispatchEvent(new Event(UIEventTypes.START_SCROLL));
		}
		
		private function onMouseDown( event:MouseEvent ):void{
			dispatchEvent(new Event(UIEventTypes.STOP_SCROLL));
		}
		
		public function get trackAsset() : Sprite {
			return _trackAsset;
		}
		
		public function set trackAsset(trackAsset : Sprite) : void {
			if(_trackAsset){
				 ErrorManager.throwError(UIErrorTypes.ASSIGN_MULTIPLE_ASSETS) 
			} else {
				if(_trackAsset){
					_trackAsset.removeEventListener(MouseEvent.CLICK, onTrackClick);
				}	
				 _trackAsset = trackAsset;
				_trackAsset.addEventListener(MouseEvent.CLICK, onTrackClick);
				if(thumbAsset){
					setLimits();
				}
			}
		}
		
		public function setLimits():void {
			thumb.area = area = new Rectangle( trackAsset.x, trackAsset.y, 0, trackAsset.height - thumbAsset.height );
			upperLimit = trackAsset.y;
			range = trackAsset.height - thumbAsset.height; 
		}
		
		public function set ratio(percent:Number):void{
			resizeThumb(percent);	
		}
		
		public function set moveTo(percent:Number):void{
			thumbAsset.y = trackAsset.y + (range*percent);
			dispatch();	
		}
		
		
		public function nudgeDown(percent:Number):void{
			thumbAsset.y += range*percent;
			if(thumbAsset.y>range+trackAsset.y){
				thumbAsset.y = range+trackAsset.y;
			}
			calculatePercent();
		}
		
		public function nudgeUp(percent:Number):void{
			thumbAsset.y -= range*percent;
			if(thumbAsset.y<0+trackAsset.y){
				thumbAsset.y = 0+trackAsset.y;	
			}
			calculatePercent();
		}
		
		
		
		/*public function sizeCalc(contentSize:Number, scrollAreaSize:Number):void{
			var percent:Number = scrollAreaSize/contentSize;	
		}*/
		
		private function resizeThumb(percent:Number):void{
			if(percent>=1){
				percent = 1;	
			}	
			thumbAsset.height = trackAsset.height*percent;
			if(thumbAsset.height<thumbAsset.width) thumbAsset.height = thumbAsset.width;
			setLimits();		
		}
		
		private function onScroll(event:Event):void{
			calculatePercent();
		}
		
		private function calculatePercent():void{
			percent = ( thumbAsset.y - trackAsset.y ) / range;
		}
		
		protected function dispatch():void{
			dispatchEvent(new RequestEvent (UIEventTypes.ON_SCROLL,false,false,{percent:percent}));	
		}
		
		protected function onTrackClick( event:Event ):void{
			
			percent = ((trackAsset.mouseY*trackAsset.scaleY) / trackAsset.height );
			
		}
		
	}
}
